﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CuaHangLego.Model;
using CuaHangLego.DataAccess;
using CuaHangLego.Bussiness;
using System.Threading;
using CuaHangLego.Data;

namespace CuaHangLego.UI
{
    public partial class QLSanPhamUI : UserControl
    {
        public SanPhamKhoBSS bss;

        public QLSanPhamUI()
        {
            InitializeComponent();
            tblDSSP.AutoGenerateColumns = false;
            this.bss = new SanPhamKhoBSS();
        }

        private void QLSanPhamUI_Load(object sender, EventArgs e)
        {
            /*CuaHangLegoDataClassesDataContext db = new CuaHangLegoDataClassesDataContext();
              var list = from sp in db.SANPHAMs
                           select sp;*/
            bss.HideInVisible();
            if (!backgroundWorker1.IsBusy)
            {
                this.backgroundWorker1.RunWorkerAsync();
            }
        }

        private void buttonAddSP_Click(object sender, EventArgs e)
        {
            SanPhamDetail f = new SanPhamDetail(bss, null);
            f.StartPosition = FormStartPosition.CenterParent;
            f.ShowDialog();
            tblDSSP.DataSource = null;
            tblDSSP.DataSource = bss.ListSP;
        }

        private void tblDSSP_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            SanPhamKho spk = tblDSSP.SelectedRows[0].DataBoundItem as SanPhamKho;
            SanPhamDetail f = new SanPhamDetail(bss, spk);
            f.StartPosition = FormStartPosition.CenterParent;
            f.ShowDialog();
            tblDSSP.DataSource = null;
            tblDSSP.DataSource = bss.ListSP;
        }

        private void buttonUpdateSP_Click(object sender, EventArgs e)
        {
            bss.UpdateData();
            tblDSSP.DataSource = null;
            tblDSSP.DataSource = bss.ListSP;
        }

        private void buttonEditSP_Click(object sender, EventArgs e)
        {
            if (tblDSSP.SelectedRows.Count > 0)
            {
                for (int i = 0; i < tblDSSP.SelectedRows.Count; i++)
                {
                    SanPhamKho spk = tblDSSP.SelectedRows[i].DataBoundItem as SanPhamKho;
                    SanPhamDetail f = new SanPhamDetail(bss, spk);
                    f.StartPosition = FormStartPosition.CenterParent;
                    f.ShowDialog();
                    tblDSSP.DataSource = null;
                    tblDSSP.DataSource = bss.ListSP;

                }
            }
        }

        private void buttonRemoveSP_Click(object sender, EventArgs e)
        {
            if (tblDSSP.SelectedRows.Count > 0)
            {
                int idx=tblDSSP.SelectedRows[0].Index;
                for (int i = 0; i < tblDSSP.SelectedRows.Count; i++)
                {
                    bss.Remove(tblDSSP.SelectedRows[i].DataBoundItem as SanPhamKho);
                }
                tblDSSP.DataSource = null;

                tblDSSP.DataSource = bss.ListSP;
            }
        }

        private void UpdateColorRow()
        {
            for (int i = 0; i < tblDSSP.Rows.Count; i++)
            {
                if ((tblDSSP.Rows[i].DataBoundItem as SanPhamKho).StateType == BaseModel.StateTypes.WillBeRemove)
                    tblDSSP.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
                else if ((tblDSSP.Rows[i].DataBoundItem as SanPhamKho).StateType == BaseModel.StateTypes.WillBeAdd)
                    tblDSSP.Rows[i].DefaultCellStyle.ForeColor = Color.Green;
                else if ((tblDSSP.Rows[i].DataBoundItem as SanPhamKho).StateType == BaseModel.StateTypes.WillBeChange)
                    tblDSSP.Rows[i].DefaultCellStyle.ForeColor = Color.Orange;
                else if ((tblDSSP.Rows[i].DataBoundItem as SanPhamKho).IsVisible == false)
                    tblDSSP.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
            }
            txtCount.Text = tblDSSP.RowCount.ToString();
        }

        private void buttonUploadSP_Click(object sender, EventArgs e)
        {
            try
            {
                bss.Commit();
                MessageBox.Show("Upload thành công!");
            }
            catch
            {
                MessageBox.Show("Đã xảy ra lỗi!");
            }
            bss.UpdateData();
            tblDSSP.DataSource = null;
            tblDSSP.DataSource = bss.ListSP;
        }

        private void buttonExportSP_Click(object sender, EventArgs e)
        {
            bss.Export();
        }

        private void buttonSearchSP_Click(object sender, EventArgs e)
        {
            tblDSSP.DataSource = bss.GetSanPhamBy(txtKey.Text);
        }

        private void buttonHienTatCaSP_Click(object sender, EventArgs e)
        {
            tblDSSP.DataSource = bss.GetAll();
            txtKey.Text = "";
        }

        private void button_boLoc_Click(object sender, EventArgs e)
        {
            txtKey.Text = "";
            new BoLocUI(tblDSSP, bss).Show();
        }

        private void checkBoxShowVisible_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxShowVisible.Checked) bss.ShowInVisible();
            else bss.HideInVisible();
            bss.UpdateData();
            tblDSSP.DataSource = null;
            tblDSSP.DataSource = bss.GetSanPhamBy(txtKey.Text);
        }

        private void tblDSSP_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            UpdateColorRow();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            bss.UpdateData();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            tblDSSP.DataSource = bss.ListSP;
        }

    }
}
